package com.bdf.ipharma.gui;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;

import org.apache.log4j.Logger;

import oracle.jdeveloper.layout.GridBagConstraints2;

import com.bdf.ipharma.communication.Task;
import com.bdf.ipharma.operation.Operation;
import com.bdf.ipharma.operation.OperationExtraer;
import com.bdf.ipharma.starter.McdError;
import com.bdf.ipharma.starter.McdMain;

public class GUIExtraccionProd extends GUIWindow{

	static final Logger logger = Logger.getLogger(GUIExtraccionProd.class);

	
	public GUIExtraccionProd(McdMain pMain) {
		super(pMain);
	      try {
	         jbInit();
	      } catch (Exception e) {
	    	  logger.fatal("GUIExtraccionProd :" + e.getMessage());
	         McdError.add(e);
	      }
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	   /**
	    * The Layouts.
	    */

	   GridBagLayout layoutImage = new GridBagLayout();
	   GridBagLayout layoutButtons = new GridBagLayout();

	   BorderLayout layoutMenu = new BorderLayout();

	   CardLayout layoutBase = new CardLayout();
	   
	   /**
	    * The main panel.
	    */
	   JPanel jPanelBase = new JPanel();

	   JPanel jPanelButtons = new JPanel();
	 
	   JPanel jPanelImage = new JPanel();

	   /**
	    * 
	    */
	   GUIButtonImage jButtExtraccion;

	   GUIButtonImage jButtCancel;
	   
	   GUIButtonImage jButtonExit;
	   
	   JLabel leyendaExtraer;
	   
		   /**
	    * Initializes the state of this instance.
	    */
	   private void jbInit() throws Exception {

	      this.setLayout(layoutMenu);
	      this.setBackground(Colors.MNU_BACK);
	      this.setRequestFocusEnabled(false);
	      
	      jButtonExit = new GUIButtonImage(Images.buttExit, Images.buttExitDn);
	      
	      jButtExtraccion = new GUIButtonImage(Texts.BTN_EXTRACCION, Images.buttOk,
	              Images.buttOkDn);
	      
	      leyendaExtraer = new JLabel("Esta funcion extraera 1 Producto del Rulo 1 de la Bandeja 1");

	      
	      
	      jButtCancel = new GUIButtonImage(Texts.BTN_CANCEL, Images.buttOk,
	              Images.buttOkDn);
	      
	      
	      /*
	       * Action Listeners
	       */
	      jButtExtraccion.addActionListener(new GUIExtraccionProd_jButtExtraccion_actionAdapter(this));
	      jButtCancel.addActionListener(new GUIExtraccionProd_jButtCancelar_actionAdapter(this));
	      
	      jPanelBase.add(jButtExtraccion, new GridBagConstraints2(0, 0, 0, 1, 0.0, 0.0,
	              GridBagConstraints.EAST, GridBagConstraints.NONE, Texts.zeroMargin,
	              0, 0));
	      
	      jPanelBase.add(leyendaExtraer, new GridBagConstraints2(1, 1, 1, 2, 0.0, 0.0,
	              GridBagConstraints.EAST, GridBagConstraints.NONE, Texts.zeroMargin,
	              0, 0));
	      

	      
	      jPanelBase.add(jButtCancel, new GridBagConstraints2(0, 0, 1, 1, 0.0, 0.0,
	              GridBagConstraints.EAST, GridBagConstraints.NONE, Texts.zeroMargin,
	              0, 0));
	      
	      this.add(jPanelImage, BorderLayout.NORTH);
	      this.add(jPanelBase, BorderLayout.CENTER);	
	      
	   }

		public void menu(String action) {
			logger.debug("INICIO --------MENU--- Extraccion Prod--------------");
			
			
			Integer nivel = 1;
			Integer posicionRulo = 1;
			Integer cantVueltas = 1;
			
			if(action.equalsIgnoreCase(Texts.BTN_EXTRACCION)){
				logger.info("Configurando TASK para Extraer Operaciones...");
				
				Task task = new Task(this.mcdMain);
				List<Operation> operations = new ArrayList<Operation>();
				OperationExtraer extraer = new OperationExtraer(cantVueltas,posicionRulo,nivel);
				extraer.setCountOperation(new Long(1));
				operations.add(extraer);
				task.setIdentificador(5);
				logger.debug("SE BUSCA PUERTO DE MCDMAIN :" + super.mcdMain.getPort());
				
				task.setPort(super.mcdMain.getPort());
				
				task.setTimeInterOperation(new Long(1));
				task.setOperations(operations);
				
				logger.debug("Se ejecuta la orden mediante el protocolo");
				
				System.out.println("Se ejecuta la orden mediante el protocolo");
				task.execute();
			}
			
			if(action.equalsIgnoreCase(Texts.BTN_CANCEL)){
				super.mcdMain.setWindow(new GUIExtraccionReposicion(super.mcdMain));
			}
			logger.debug("FIN-----MENU-----------Extraccion Prod------");
			
			
			try {
				
			} catch (Exception e) {
				logger.fatal("GUIEXTRACCIONPROD : " + e.getMessage());
				
			}

		}
	   
	   
	   public void jButtExtraccion_actionPerformed(ActionEvent e) {
		this.menu(e.getActionCommand());
	   }

		public void jButtCancel_actionPerformed(ActionEvent e) {
			this.menu(e.getActionCommand());
		}
	 
}

class GUIExtraccionProd_jButtExtraccion_actionAdapter implements
		java.awt.event.ActionListener {

	GUIExtraccionProd adaptee;

	GUIExtraccionProd_jButtExtraccion_actionAdapter(GUIExtraccionProd adaptee) {

		this.adaptee = adaptee;
	}

	public void actionPerformed(ActionEvent e) {

		adaptee.jButtExtraccion_actionPerformed(e);
	}
}

class GUIExtraccionProd_jButtCancelar_actionAdapter implements
		java.awt.event.ActionListener {

	GUIExtraccionProd adaptee;

	GUIExtraccionProd_jButtCancelar_actionAdapter(GUIExtraccionProd adaptee) {

		this.adaptee = adaptee;
	}

	public void actionPerformed(ActionEvent e) {

		adaptee.jButtCancel_actionPerformed(e);
	}
}



